import 'package:flutter/material.dart';
import 'package:flutter_base/config/app_config.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_base/base/widget/base_get_builder.dart';
import 'package:flutter_base/widgets/appbar/common_app_bar.dart';
import 'package:flutter_base/config/app_color.dart';
import 'package:flutter_base/widgets/switch/switch_widget.dart';
import 'package:flutter_base/widgets/image/auto_image.dart';
import 'package:flutter_base/generated/assets.dart';
import 'setting_logic.dart';

class SettingPage extends StatelessWidget {
  final logic = Get.put(SettingLogic());

  SettingPage({super.key});

  @override
  Widget build(BuildContext context) {
    return BaseGetBuilder<SettingLogic>(
      appBar: const CommonAppBar(
        title: 'Settings',
        // showLeft: true,
      ),
      extendBodyBehindAppBar: true,

      backgroundColor: AppColor.colorF5F5F5,
      builder: (controller) {
        return Container(
          padding: EdgeInsets.only(top: ScreenUtil().statusBarHeight + kToolbarHeight),
          decoration: const BoxDecoration(
              image: DecorationImage(
                  image: AssetImage(Assets.mineBg),
                  fit: BoxFit.fitWidth,
                  alignment: Alignment.topCenter)),
          child: SingleChildScrollView(
            physics: const BouncingScrollPhysics(),
            child: Column(
              children: [
                // 顶部应用信息
                _buildAppInfoSection(),
                SizedBox(height: 20.w),
                // 账户设置卡片
                _buildSettingCard([
                  _buildSettingItem(
                    'Account Settings',
                    onTap: () => controller.onAccountSettingsTap(),
                  ),
                  _buildSettingItem(
                    'Contact Customer Service',
                    onTap: () => controller.onContactCustomerServiceTap(),
                  ),
                  _buildSettingItem(
                    'User Agreement',
                    onTap: () => controller.onUserAgreementTap(),
                  ),
                  _buildSettingItem(
                    'Privacy Policy',
                    onTap: () => controller.onPrivacyPolicyTap(),
                  ),
                  _buildSettingItem(
                    'Disclaimer',
                    onTap: () => controller.onDisclaimerTap(),
                  ),
                  _buildSettingItemWithSwitch(
                    'Auto-Renewal',
                    isOn: controller.isAutoRenewalEnabled.value,
                    onChanged: (value) => controller.toggleAutoRenewal(value),
                  ),
                ]),
                SizedBox(height: 20.w),
                // 其他设置卡片
                _buildSettingCard([
                  _buildSettingItem(
                    'Language Switch',
                    suffix: Text(
                      'Chinese',
                      style: TextStyle(
                        color: AppColor.color0C0A09.withOpacity(0.16),
                        fontSize: 12.sp,
                        fontFamily: 'Outfit',
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                    onTap: () => controller.onLanguageSwitchTap(),
                  ),
                  _buildSettingItem(
                    'Check for Updates',
                    onTap: () => controller.onCheckForUpdatesTap(),
                  ),
                  _buildSettingItem(
                    'Clear Cache',
                    suffix: Text(
                      '1.12M',
                      style: TextStyle(
                        color: AppColor.color0C0A09.withOpacity(0.16),
                        fontSize: 12.sp,
                        fontFamily: 'Outfit',
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                    onTap: () => controller.onClearCacheTap(),
                  ),
                ]),
                SizedBox(height: 20.w),
                // 退出登录按钮
                _buildLogoutButton(),
                SizedBox(height: 20.w),
              ],
            ),
          ),
        );
      },
    );
  }

  // 应用信息区域
  Widget _buildAppInfoSection() {
    return SizedBox(
      width: double.infinity,
      // padding: EdgeInsets.symmetric(vertical: 10.w),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          40.verticalSpaceFromWidth,
          ClipRRect(
            borderRadius: BorderRadius.circular(12.w),
            child: AutoImage(
              Assets.logo,
              width: 80.w,
              height: 80.w,

              // borderRadius: BorderRadius.circular(20.w),
            ),
          ),
          SizedBox(height: 8.w),
          Text(
            AppConfig.appName,
            style: TextStyle(
              fontSize: 16.sp,
              fontWeight: FontWeight.bold,
              fontFamily: 'Outfit',
              color: AppColor.color0C0A09,
            ),
          ),
          SizedBox(height: 4.w),
          Text(
               'v${AppConfig.version}',
            style: TextStyle(
              fontSize: 14.sp,
              fontWeight: FontWeight.normal,
              fontFamily: 'Outfit',
              color: AppColor.color7F909E,
            ),
          ),
        ],
      ),
    );
  }

  // 设置卡片
  Widget _buildSettingCard(List<Widget> children) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16.w),
      padding: EdgeInsets.all(16.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20.w),
      ),
      child: Column(
        children: List.generate(children.length, (index) {
          final widget = children[index];
          if (index < children.length - 1) {
            return Column(
              children: [
                widget,
                SizedBox(height: 16.w),
              ],
            );
          } else {
            return widget;
          }
        }),
      ),
    );
  }

  // 设置项
  Widget _buildSettingItem(
    String title, {
    Widget? suffix,
    VoidCallback? onTap,
  }) {
    return InkWell(
      onTap: onTap,
      child: Container(
        height: 24.w,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              title,
              style: TextStyle(
                fontSize: 14.sp,
                fontWeight: FontWeight.w500,
                fontFamily: 'Outfit',
                color: AppColor.color0C0A09,
              ),
            ),
            suffix ??
                Icon(
                  Icons.chevron_right,
                  size: 24.w,
                  color: AppColor.color333333.withOpacity(0.5),
                ),
          ],
        ),
      ),
    );
  }

  // 带开关的设置项
  Widget _buildSettingItemWithSwitch(
    String title, {
    required bool isOn,
    required ValueChanged<bool>? onChanged,
  }) {
    return Container(
      height: 24.w,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            title,
            style: TextStyle(
              fontSize: 14.sp,
              fontWeight: FontWeight.w500,
              fontFamily: 'Outfit',
              color: AppColor.color0C0A09,
            ),
          ),
          SwitchWidget(
            value: isOn,
            onChanged: onChanged,
            activeColor: AppColor.primary,
            width: 48.w,
            height: 24.w,
          ),
        ],
      ),
    );
  }

  // 退出登录按钮
  Widget _buildLogoutButton() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16.w),
      width: double.infinity,
      height: 52.w,
      child: ElevatedButton(
        onPressed: () => logic.onLogoutTap(),
        style: ElevatedButton.styleFrom(
          backgroundColor: Colors.white,
          foregroundColor: AppColor.errorColor,
          elevation: 0,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(78.w),
          ),
        ),
        child: Text(
          'Log Out',
          style: TextStyle(
            fontSize: 16.sp,
            fontWeight: FontWeight.w600,
            fontFamily: 'Outfit',
          ),
        ),
      ),
    );
  }
} 